package com.wyvoid.forum.web.action.admin.forum;

import com.wyvoid.forum.Constant;
import com.wyvoid.forum.domain.Forum;
import com.wyvoid.forum.domain.User;
import com.wyvoid.forum.service.IForumService;
import com.wyvoid.forum.service.IUserService;
import com.wyvoid.forum.web.action.admin.BaseAdminAction;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-26
 * Time: 10:52:34
 */
@UrlBinding(value = "/admin/forumManage.do")
public class ForumManageAction extends BaseAdminAction {
    @SpringBean
    private IForumService forumService;
    @SpringBean
    private IUserService userService;
    private List<Forum> forums;
    private Forum forum;
    private long fid;
    private long parentId;

    public void setForumService(IForumService forumService) {
        this.forumService = forumService;
    }

    public List<Forum> getForums() {
        return forums;
    }

    public Forum getForum() {
        return forum;
    }

    public void setForum(Forum forum) {
        this.forum = forum;
    }

    public void setFid(long fid) {
        this.fid = fid;
    }

    public void setParentId(long parentId) {
        this.parentId = parentId;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @DefaultHandler
    public Resolution forumList() {
        forums = new ArrayList<Forum>();
        List<Forum> oneDegree = forumService.getForums(0);

        for (Forum oneForum : oneDegree) {
            forums.add(oneForum);
            List<Forum> oneForums = new ArrayList<Forum>();
            oneForum.setChilds(oneForums);
            List<Forum> twoDegree = forumService.getForums(oneForum.getId());
            for (Forum twoForum : twoDegree) {
                oneForums.add(twoForum);
                List<Forum> twoForums = new ArrayList<Forum>();
                twoForum.setChilds(twoForums);
                List<Forum> threeDegree = forumService.getForums(twoForum.getId());
                for (Forum threeForum : threeDegree) {
                    twoForums.add(threeForum);
                }
            }
        }

        return getAdminResolution("forum/forumList");
    }

    public Resolution addForumForm() {
        if(fid>0){
            forum=forumService.getForum(fid);
        }
        return getAdminResolution("forum/addForum");
    }

    public Resolution addForum() {
        Forum parentForum = forumService.getForum(forum.getParentId());
        forum.setDepth(parentForum.getDepth() + 1);
        forum.setPath(parentForum.getPath() + "/" + parentForum.getId());
        if (forum.getId() > 0) {
            Forum tempForum=forumService.getForum(forum.getId());
            tempForum.setName(forum.getName());
            tempForum.setDescription(forum.getDescription());
            tempForum.setViewOrder(forum.getViewOrder());
            forumService.updateForum(tempForum);
        } else {
            forum.setCreateDate(new Date());
            forum.setType(Constant.Forum.TYPE_FORUM);
            forumService.createForum(forum);
        }
        return new RedirectResolution(this.getClass());
    }

    /**
     * 添加顶层版块
     *
     * @return reso
     */
    public Resolution addGroupForm() {
        if(fid>0){
            forum=forumService.getForum(fid);
        }
        return getAdminResolution("forum/addGroup");
    }

    public Resolution addGroup() {
        if (forum.getId() > 0) {
            Forum tempForum=forumService.getForum(forum.getId());
            tempForum.setName(forum.getName());
            tempForum.setViewOrder(forum.getViewOrder());
            forumService.updateForum(tempForum);
        } else {
            forum.setCreateDate(new Date());
            forum.setType(Constant.Forum.TYPE_GROUP);
            forum.setPath("");
            forum.setDepth(0);
            forumService.createForum(forum);
        }
        return new RedirectResolution(this.getClass());
    }

    public Resolution closeForum() {
        forumService.closeForum(fid);
        return new RedirectResolution(this.getClass());
    }

    public Resolution openForum() {
        forumService.openForum(fid);
        return new RedirectResolution(this.getClass());
    }

    public Resolution moveUp(){
        List<Forum> forumList=forumService.getForums(parentId);
        int curForumIndex=getForumIndex(forumList,fid);
        if(curForumIndex>0){
            int lastForumIndex=curForumIndex-1;
            Forum curForum=forumList.get(curForumIndex);
            Forum lastForum=forumList.get(lastForumIndex);
            int curForumOrder=curForum.getViewOrder();
            int lastForumOrder=lastForum.getViewOrder();
            if(curForumOrder==lastForumOrder){
                curForumOrder=curForumOrder+1;
            }
            curForum.setViewOrder(lastForumOrder);
            lastForum.setViewOrder(curForumOrder);
            forumService.updateForum(curForum);
            forumService.updateForum(lastForum);
        }
        return new RedirectResolution(this.getClass());
    }

    public Resolution moveDown(){
        List<Forum> forumList=forumService.getForums(parentId);
        int curForumIndex=getForumIndex(forumList,fid);
        if(curForumIndex<forumList.size()-1){
            int nextForumIndex=curForumIndex+1;
            Forum curForum=forumList.get(curForumIndex);
            Forum nextForum=forumList.get(nextForumIndex);
            int curForumOrder=curForum.getViewOrder();
            int nextForumOrder=nextForum.getViewOrder();
            if(curForumOrder==nextForumOrder){
                nextForumOrder=nextForumOrder+1;
            }
            curForum.setViewOrder(nextForumOrder);
            nextForum.setViewOrder(curForumOrder);
            forumService.updateForum(curForum);
            forumService.updateForum(nextForum);
        }
        return new RedirectResolution(this.getClass());
    }

    public Resolution setMasterForm(){
        forum=forumService.getForum(fid);
        return getAdminResolution("forum/masterForm");
    }

    public Resolution setMaster(){
        Forum tempForum=forumService.getForum(forum.getId());
        String tempMasterStr=forum.getMasters();
        String[] tempMasters=tempMasterStr.split(",");
        String mastersNickName="";
        for(String str:tempMasters){
            User user=userService.getUser(Long.parseLong(str));
            mastersNickName+=str+","+user.getNickname()+"|";
        }

        tempForum.setMasters(tempMasterStr);
        tempForum.setMastersNickname(mastersNickName);
        forumService.updateForum(tempForum);
        return new RedirectResolution(this.getClass());
    }

    public Resolution addTagForm(){
        forum=forumService.getForum(fid);
        return getAdminResolution("forum/addTagForm");
    }

    public Resolution addTag(){
        Forum tempForum=forumService.getForum(forum.getId());
        tempForum.setTags(forum.getTags());
        forumService.updateForum(tempForum);
        return new RedirectResolution(this.getClass());
    }

    private int getForumIndex(List<Forum> forumList,long forumId){
        for(int i=0;i<forumList.size();i++){
            Forum tempForum=forumList.get(i);
            if(tempForum.getId()==forumId){
                return i;
            }
        }
        return 0;
    }
}
